﻿using Fur.DatabaseAccessor;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;

namespace China.Core
{
    /// <summary>
    /// 角色表
    /// </summary>
    public class Role : EntityBase, IEntityTypeBuilder<Role>, IEntitySeedData<Role>
    {
        /// <summary>
        /// 角色名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string Remark { get; set; }

        /// <summary>
        /// 拥有用户，多对多
        /// </summary>
        public ICollection<User> Users { get; set; }

        /// <summary>
        /// 关联关系表
        /// </summary>
        public List<UserRole> UserRoles { get; set; }

        /// <summary>
        /// 多个权限
        /// </summary>
        public ICollection<Security> Securities { get; set; }

        /// <summary>
        /// 关联关系表
        /// </summary>
        public List<RoleSecurity> RoleSecurities { get; set; }

        public void Configure(EntityTypeBuilder<Role> entityBuilder, DbContext dbContext, Type dbContextLocator)
        {
            entityBuilder.HasMany(u => u.Securities)
                .WithMany(u => u.Roles)
                .UsingEntity<RoleSecurity>(
                    u => u.HasOne(c => c.Security).WithMany(c => c.RoleSecurities).HasForeignKey(c => c.SecurityId),
                    u => u.HasOne(c => c.Role).WithMany(c => c.RoleSecurities).HasForeignKey(c => c.RoleId),
                    u =>
                    {
                        u.HasKey(c => new { c.RoleId, c.SecurityId });
                        u.HasData(new { RoleId = 1, SecurityId = 1 });
                    });
        }

        public IEnumerable<Role> HasData(DbContext dbContext, Type dbContextLocator)
        {
            return new[]
            {
                new Role
                {
                    Id=1, Name =" 超级管理员", Remark="最牛逼的人"
                },
                new Role
                {
                    Id=2, Name =" 开发者", Remark="一线码农"
                }
            };
        }
    }
}
